﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SurveyAdd.ascx.cs" company="hoandd">
//   Do Duc Hoan
// </copyright>
// <summary>
//   DesktopModules Surveys SurveyAdd
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DesktopModules.Surveys
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Localization;

    public partial class DesktopModules_Surveys_AddQuestion : PortalModuleBase
    {
        private int SurveyId = -1;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Request.QueryString["SurveyId"] == null))
            {
                SurveyId = int.Parse(Request.QueryString["SurveyId"]);
            }
            else
            {
                SurveyId = Null.NullInteger;
            }

            if ((Page.IsPostBack == false))
            {
                if (!Null.IsNull(SurveyId))
                {
                    SQuestioninfo objSurvey = SQuestionController.GetSQuestion(SurveyId);
                    if (!(objSurvey == null))
                    {
                        txtContent.Text = objSurvey.QuestionContent;
                        cboQuestionType.Items.FindByValue(Convert.ToString(objSurvey.QuestionType)).Selected = true;

                        List<SQuestionAnswerInfo> arrSurveyOptions = SQuestionAnswerController.GetSQuestionAnswers(SurveyId);

                        SQuestionAnswerInfo SQuestionAnswer;
                        int intIndex;
                        for (intIndex = 0; (intIndex <= (arrSurveyOptions.Count - 1)); intIndex++)
                        {
                            SQuestionAnswer = ((SQuestionAnswerInfo)(arrSurveyOptions[intIndex]));
                            arrSurveyOptions[intIndex] = SQuestionAnswer;
                        }
                        lstOptions.DataSource = arrSurveyOptions;
                        lstOptions.DataBind();
                    }
                    else
                    {
                        //  security violation attempt to access item not related to this Module
                        Response.Redirect(Globals.NavigateURL());
                    }
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SQuestioninfo objQuestion = new SQuestioninfo();
            objQuestion.SQuestionID = SurveyId;
            objQuestion.OrgID = 4;
            objQuestion.ModuleID = ModuleId;
            objQuestion.QuestionType = (byte)Convert.ToInt32(cboQuestionType.SelectedItem.Value);
            objQuestion.QuestionContent = this.txtContent.Text;
            if (publicCheckBox.Checked)
                objQuestion.Public = true;
            else
                objQuestion.Public = false;
            //string []s = DateTime.Now.GetDateTimeFormats();
            objQuestion.DateCreated = DateTime.Now;
            objQuestion.LastModified = DateTime.Now;
            objQuestion.CreatedByUserID = UserId;
            if (Null.IsNull(SurveyId))
            {
                SurveyId = SQuestionController.AddSQuestion(objQuestion);
                int intOption;
                for (intOption = 0; (intOption <= (lstOptions.Items.Count - 1)); intOption++)
                {
                    SQuestionAnswerInfo objQuestionAnswer = new SQuestionAnswerInfo();
                    objQuestionAnswer.SQuestionID = SurveyId;
                    objQuestionAnswer.Answer = lstOptions.Items[intOption].Text;
                    objQuestionAnswer.ViewOrder = intOption;

                    SQuestionAnswerController.AddSQuestionAnswer(objQuestionAnswer);
                    //    SQuestionAnswerController.UpdateSQuestionAnswer(objQuestionAnswer);
                }
            }
            else
            {
                objQuestion.LastModified = DateTime.Now;
                SQuestionController.UpdateSQuestion(objQuestion);
                int intOption;
                for (intOption = 0; (intOption <= (lstOptions.Items.Count - 1)); intOption++)
                {
                    SQuestionAnswerInfo objQuestionAnswer = new SQuestionAnswerInfo();
                    objQuestionAnswer.SAnswerID = int.Parse(lstOptions.Items[intOption].Value);
                    objQuestionAnswer.SQuestionID = SurveyId;
                    objQuestionAnswer.Answer = lstOptions.Items[intOption].Text;
                    objQuestionAnswer.ViewOrder = intOption;

                    SQuestionAnswerController.UpdateSQuestionAnswer(objQuestionAnswer);
                }
            }

            Response.Redirect(Globals.NavigateURL());
        }

        /// <summary>
        /// Handles the Click event of the btnReferch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnReferch_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Globals.NavigateURL());
        }

        /// <summary>
        /// Handles the Click event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the AddAnswerButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AddAnswerButtonClick(object sender, EventArgs e)
        {
            if (txtAnswer.Text != string.Empty)
            {
                bool blnIsUnique = true;
                int intItem;
                //for (intItem = 0; intItem <= (lstOptions.Items.Count - 1); intItem++)
                //{
                //    if (this.lstOptions.Items[intItem].Text.ToLower().CompareTo(this.txtAnswer.Text.ToLower()) != 0)
                //    {
                //        blnIsUnique = false;
                //    }
                //}

                if (blnIsUnique)
                {
                    string strItem = Guid.NewGuid().ToString();
                    string strOption = txtAnswer.Text;
                    lstOptions.Items.Add(new ListItem(strOption, strItem));
                    // chkCorrect.Checked = false;
                }

                // else
                // {
                // // DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("OptionExists", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
                // }
                txtAnswer.Text = string.Empty;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdDeleteOption control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void cmdDeleteOption_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            // SurveyOptionInfo objSurveyOptionInfo = new SurveyOptionInfo();
            if (lstOptions.SelectedIndex != -1)
            {
                lstOptions.Items.RemoveAt(lstOptions.SelectedIndex);
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdDown control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void cmdDown_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (lstOptions.SelectedIndex < (lstOptions.Items.Count - 1))
            {
                // save
                int intSelectedIndex = lstOptions.SelectedIndex;
                ListItem objListItem = lstOptions.Items[lstOptions.SelectedIndex];

                // remove
                lstOptions.Items.Remove(objListItem);

                // move up
                lstOptions.Items.Insert((intSelectedIndex + 1), objListItem);
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdUp control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void cmdUp_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (lstOptions.SelectedIndex > 0)
            {
                // save
                int intSelectedIndex = lstOptions.SelectedIndex;
                ListItem objListItem = lstOptions.Items[lstOptions.SelectedIndex];

                // remove
                lstOptions.Items.Remove(objListItem);

                // move up
                lstOptions.Items.Insert((intSelectedIndex - 1), objListItem);
            }
        }

        protected void cboQuestionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboQuestionType.SelectedValue == "1")
            {
                divAnser.Visible = true;
            }
            else
                divAnser.Visible = false;
        }
    }
}